package com.garlic.face.jlf.ui;

import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;


import com.garlic.face.jlf.R;

import java.lang.reflect.Type;
import java.util.concurrent.Callable;

public class NormalDialog extends DialogFragment {

    private String type;
    private String title;
    private String content;

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public void setCallBackTwo(CallBackTwo callBackTwo) {
        this.callBackTwo = callBackTwo;
    }


    private CallBack callBack;

    private CallBackTwo callBackTwo;

    public static NormalDialog newInstanceOne(String title,String content){
        NormalDialog normalDialog=new NormalDialog();
        Bundle bundle=new Bundle();
        bundle.putString("type","one");
        bundle.putString("title",title);
        bundle.putString("content",content);
        normalDialog.setArguments(bundle);
        return normalDialog;
    }

    public static NormalDialog newInstanceTwo(String title,String content){
        NormalDialog normalDialog=new NormalDialog();
        Bundle bundle=new Bundle();
        bundle.putString("type","two");
        bundle.putString("title",title);
        bundle.putString("content",content);
        normalDialog.setArguments(bundle);
        return normalDialog;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
          type=getArguments().getString("type","one");
          title=getArguments().getString("title");
          content=getArguments().getString("content");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        if (type.equals("one")){
            view=inflater.inflate(R.layout.popup_window_errormsg,container,false);
            TextView errorTitle=view.findViewById(R.id.popup_window_error_title);
            TextView error_content=view.findViewById(R.id.popup_window_error_content);
            Button error_ok=view.findViewById(R.id.popup_window_error_ok);
             errorTitle.setText(title);
             error_content.setText(content);
             error_ok.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                       callBack.oneClick(v);
                 }
             });

        }else {
             view=inflater.inflate(R.layout.popup_window_confirm,container,false);
            TextView confirm_title=view.findViewById(R.id.popup_window_confirm_title);
            TextView confirm_message=view.findViewById(R.id.popup_window_confirm_message);
            Button button_ok=view.findViewById(R.id.popup_window_confirm_button_ok);
            Button button_cancel=view.findViewById(R.id.popup_window_confirm_button_cancel);
            confirm_title.setText(title);
            confirm_message.setText(content);

            button_ok.setOnClickListener((v)->{
                  callBackTwo.twoOk(v);
            });

            button_cancel.setOnClickListener((view1)->{
                 callBackTwo.twoCancel(view1);
            });
        }
        return view;
    }

    public  interface CallBack{
        void oneClick(View view);
    }
    public interface CallBackTwo{
        void twoOk(View view);
        void twoCancel(View view);
    }
}
